package org.zanateh.wargame.graphics;

import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;

import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.entity.shape.IShape;
import org.anddev.andengine.entity.shape.Shape;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.util.GLHelper;
import org.zanateh.wargame.game.hexagon.Hexagon;
import org.zanateh.wargame.graphics.card.Card;

public class HexagonDrawable extends Shape {

	
	protected HexagonVertexBuffer mVertexBuffer = null;
	protected float mSideLength = 1;
	boolean mGrabbed = false;
	float mGrabOffsetX = 0f;
	float mGrabOffsetY = 0f;
	
	private Hexagon mHex;
	
	public HexagonDrawable(Hexagon hex) {
		super(0, 0);

		mHex = hex;

		this.mRotationCenterX = 0f;
		this.mRotationCenterY = 0f;

		this.mScaleCenterX = this.mRotationCenterX;
		this.mScaleCenterY = this.mRotationCenterY;
		
	}
	
	public void setPositionAndSize(float pX, float pY, float sideLength)
	{
		setPosition(pX, pY);
		mSideLength = sideLength;

		this.mVertexBuffer = new HexagonVertexBuffer(GL11.GL_STATIC_DRAW, true);
        onUpdateVertexBuffer();
	}
	
	public Hexagon getHexagon()
	{
		return mHex;
	}
	
	@Override
	public HexagonVertexBuffer getVertexBuffer() {
		if(this.mVertexBuffer == null) {
			throw new NullPointerException("No vertex buffer until drawable is given position and size!");
		}
		return this.mVertexBuffer;
	}

	@Override
	protected void onInitDraw(final GL10 pGL) {
		super.onInitDraw(pGL);
		GLHelper.disableTextures(pGL);
		GLHelper.disableTexCoordArray(pGL);
	}
	
	@Override
	protected void onUpdateVertexBuffer() {
		this.getVertexBuffer().update(mSideLength);
	}

	@Override
	public float getWidth() {
		// TODO Auto-generated method stub
		return mSideLength*2;
	}

	@Override
	public float getHeight() {
		// TODO Auto-generated method stub
		return mSideLength*2;
	}

	@Override
	public float getBaseWidth() {
		// TODO Auto-generated method stub
		return mSideLength*2;
	}

	@Override
	public float getBaseHeight() {
		// TODO Auto-generated method stub
		return mSideLength*2;
	}

	@Override
	public boolean collidesWith(IShape pOtherShape) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean contains(final float pX, final float pY) {
		// TODO Auto-generated method stub
		return HexagonalShapeCollisionChecker.checkContains(this, pX, pY);
	}

	@Override
	protected void drawVertices(GL10 pGL, Camera pCamera) {
		pGL.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, HexagonVertexBuffer.VERTICES_PER_HEXAGON);
	}

	@Override
	protected boolean isCulled(Camera pCamera) {
		// TODO Auto-generated method stub
		return false;
	}
	
	@Override
	public float[] getSceneCenterCoordinates() {
		return this.convertLocalToSceneCoordinates(0f, 0f);
	}
	
	public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
		switch(pSceneTouchEvent.getAction()) {
			case TouchEvent.ACTION_DOWN:
				this.setScale(1.25f);
				this.mGrabbed = true;
				mGrabOffsetX = pTouchAreaLocalX;
				mGrabOffsetY = pTouchAreaLocalY;
				break;
			case TouchEvent.ACTION_MOVE:
//				if(this.mGrabbed) {
//					this.setPosition(pSceneTouchEvent.getX() - mGrabOffsetX, pSceneTouchEvent.getY() - mGrabOffsetY);
//				}
				break;
			case TouchEvent.ACTION_UP:
				if(this.mGrabbed) {
					this.mGrabbed = false;
					this.setScale(1.0f);
				}
				break;
		}
		return true;
	}


	// TODO: write the update and use the sidelength.

}
